@echo off

cls

call config.bat

rem /////////////// SET SOME PATHS ////////////////

	set PATH_PROJ=%cd%
	set PATH_DATA=%PATH_PROJ%\res
	set PATH_SRC=%PATH_PROJ%\src
	set PATH_TOOLS=%PATH_PROJ%\tools
	set PATH_TEMP=%PATH_PROJ%\_temp
	set PATH_RELEASE=%PATH_PROJ%\_release
	set PATH_JAVALIB=%WTK_PATH%\lib
	
	set PATH_TEMP_DATA=%PATH_TEMP%\res
	set PATH_TEMP_SRC=%PATH_TEMP%\src
	
	set PATH_SRC_RAW=%PATH_TEMP_SRC%\0_raw
	set PATH_SRC_COMPILED=%PATH_TEMP_SRC%\1_compiled
	set PATH_SRC_OBFUSCATED=%PATH_TEMP_SRC%\2_obfuscated
	set PATH_SRC_PREVERIFIED=%PATH_TEMP_SRC%\3_preverified
	
	set CLASSPATH=%PATH_JAVALIB%\midpapi20.jar;%PATH_JAVALIB%\cldcapi11.jar
	
	set VERSION=%VERSION_MAJOR%%VERSION_MINOR%%VERSION_BUILD%
	set VERSION_DOT=%VERSION_MAJOR%.%VERSION_MINOR%.%VERSION_BUILD%
	  
	set PROJ_FULL_NAME=%PROJ_NAME%_v%VERSION%
	set MAIN_CLASS_NAME=com.gameloft.futbol.%PROJ_NAME%
	set MAIN_CLASS_FILE=%MAIN_CLASS_NAME:.=\%.java
	
rem /////////////// CLEAN UP ////////////////
  
  echo.
  echo ----- Cleaning up
  
  cd %PATH_PROJ%
  
  if not exist %PATH_RELEASE% mkdir %PATH_RELEASE%
  
  rd /q / s %PATH_TEMP% 1> NUL 2> NUL
  mkdir %PATH_TEMP%
  mkdir %PATH_TEMP_DATA%
  mkdir %PATH_TEMP_SRC%
  mkdir %PATH_SRC_RAW%
  mkdir %PATH_SRC_COMPILED%
  mkdir %PATH_SRC_OBFUSCATED%
  mkdir %PATH_SRC_PREVERIFIED%

rem /////////////// DATA ////////////////
  
  echo.
  echo ----- Processing data
  
  copy %PATH_DATA%\* %PATH_TEMP_DATA% 1> NUL 2> NUL

rem /////////////// SOURCE ////////////////
  
  echo.
  echo ----- Processing source
  echo.
  
  echo    - Copying raw sources
  echo.
  xcopy /s /y %PATH_SRC%\*.java %PATH_SRC_RAW%\ > NUL
  
  echo    - Compiling
  echo.
  cd %PATH_SRC_RAW%
  "%JDK_PATH%\bin\javac.exe" -g -classpath %CLASSPATH% -sourcepath %PATH_SRC_RAW% -d %PATH_SRC_COMPILED% -encoding UTF-8 -source 1.3 -target 1.1 %MAIN_CLASS_FILE%
  if not %ERRORLEVEL%==0 (
    echo ERROR:: error while compiling
    goto error
  )
  
  if "%OBFUSCATE%"=="1" (
    echo    - Jaring class files
    echo.
    cd %PATH_TEMP_SRC%
    "%JDK_PATH%\bin\jar.exe" cfM "%PROJ_FULL_NAME%.jar" -C "%PATH_SRC_COMPILED%" .
    if not %ERRORLEVEL%==0 (
      echo ERROR:: error while jaring class files
      goto error
    )
    echo    - Obfuscating
    echo -libraryjars %CLASSPATH%                               > proguard.pro
    echo -injars %PROJ_FULL_NAME%.jar                           >> proguard.pro
    echo -outjar %PATH_SRC_OBFUSCATED%\%PROJ_FULL_NAME%.jar     >> proguard.pro
    echo -keep public class %PROJ_NAME%                     		>> proguard.pro
    echo -printmapping %PROJ_FULL_NAME%.mapping.txt             >> proguard.pro
  	echo -overloadaggressively 																	>> proguard.pro
	  %JDK_PATH%\bin\java.exe -jar "%PATH_TOOLS%\proguard\proguard.jar" @proguard.pro 1> proguard.log 2> NUL
    if not %ERRORLEVEL%==0 (
      echo ERROR:: error while obfuscating
      goto error
    )
    del %PROJ_FULL_NAME%.jar
    cd %PATH_SRC_OBFUSCATED%
    "%JDK_PATH%\bin\jar" -xf "%PROJ_FULL_NAME%.jar"
    del "%PROJ_FULL_NAME%.jar"
    if not %ERRORLEVEL%==0 (
      echo ERROR:: error while obfuscating
      goto error
    )
  ) else (
    echo    - Dont obfuscate
    xcopy /s /y %PATH_SRC_COMPILED%\* %PATH_SRC_OBFUSCATED% > NUL
  )
  
  echo.
  echo    - Preverifying
  echo.
  "%WTK_PATH%\bin\preverify" -target CLDC1.1 -classpath %CLASSPATH% -d %PATH_SRC_PREVERIFIED% %PATH_SRC_OBFUSCATED%
  if not %ERRORLEVEL%==0 (
    echo ERROR:: error while preverifying
    goto error
  )

rem /////////////// MANIFEST ///////////////
  
  echo ----- Creating manifest
  echo.
  
  cd %PATH_TEMP_SRC%
  echo Manifest-Version: 1.0> MANIFEST.MF
  echo MicroEdition-Profile: MIDP-2.0>> MANIFEST.MF
  echo MicroEdition-Configuration: CLDC-1.0>> MANIFEST.MF
  echo MIDlet-Vendor: Gameloft SA>> MANIFEST.MF
  echo MIDlet-Name: %PROJ_NAME%>> MANIFEST.MF
  echo MIDlet-Version: %VERSION_DOT%>>MANIFEST.MF
  echo MIDlet-Icon: /icon.png>> MANIFEST.MF
  echo MIDlet-1: %PROJ_NAME%, /icon.png, %MAIN_CLASS_NAME%>> MANIFEST.MF

rem /////////////// CREATING JAR ///////////////
  
  echo ----- Creating JAR file
  echo.
  
  "%JDK_PATH%\bin\jar.exe" cmf MANIFEST.MF "%PROJ_FULL_NAME%.jar" -C "%PATH_TEMP_DATA%" .
  if not %ERRORLEVEL%==0 (
    echo ERROR:: error while jaring data and manifest
    goto error
  )
  
  "%JDK_PATH%\bin\jar.exe" uf "%PROJ_FULL_NAME%.jar" -C "%PATH_SRC_PREVERIFIED%" .
  if not %ERRORLEVEL%==0 (
    echo ERROR:: error while jaring classes
    goto error
  )
  
rem /////////////// JAD FILE ///////////////
  
  echo ----- Creating JAD file
  echo.
  
  cd %PATH_TEMP_SRC%
  echo MIDlet-Vendor: Gameloft SA> %PROJ_FULL_NAME%.jad
  echo MIDlet-Name: %PROJ_NAME%>> %PROJ_FULL_NAME%.jad
  echo MIDlet-Version: %VERSION_DOT%>> %PROJ_FULL_NAME%.jad
  echo MIDlet-Icon: /icon.png>> %PROJ_FULL_NAME%.jad  
  echo MIDlet-1: %PROJ_NAME%, /icon.png, %MAIN_CLASS_NAME%>> %PROJ_FULL_NAME%.jad
  echo MIDlet-Description: Midi mixer test>> %PROJ_FULL_NAME%.jad
  echo MIDlet-Jar-URL: %PROJ_FULL_NAME%.jar>> %PROJ_FULL_NAME%.jad
  "%JDK_PATH%\bin\java.exe" -cp "%PATH_TOOLS%\jarSize" jarSize %PROJ_FULL_NAME%.jar %PROJ_FULL_NAME%.jad
  if not %ERRORLEVEL%==0 (
    echo ERROR:: error while creating JAD file
    goto error
  )

rem /////////////// EXPORTING JAD & JAR FILES ///////////////
  
  echo ----- Exporting JAD and JAR files to release directory
  echo.
  
  move %PATH_TEMP_SRC%\%PROJ_FULL_NAME%.jad %PATH_RELEASE% 1> NUL 2> NUL
  move %PATH_TEMP_SRC%\%PROJ_FULL_NAME%.jar %PATH_RELEASE% 1> NUL 2> NUL
  if not %ERRORLEVEL%==0 (
    echo ERROR:: error while exporting JAD and JAR files
    goto error
  )
  
	goto end
  
:error
echo build process stopped, an error occured
cd %PATH_PROJ%

:end
cd %PATH_PROJ%